package com.cardbaobao.cardbabyclient.utils;

import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.TextView;

/**
 * SpanUtil
 * Created by zad
 * 2016/11/15 10:05
 */

public class SpanUtil {

    /**
     * 部分文字单击接口
     */
    public interface SpanClickListener {
        void onSpanClick(View v);
    }

    /**
     * 设置文本单个或多个字段变色
     *
     * @param textView
     * @param color    要变的颜色
     * @param subStr  子文本(会被拆分)
     */
    public static void setMultipleTextFieldChangeColorByString(final TextView textView, final int color, String subStr){
        if(!TextUtils.isEmpty(subStr)){
            String str = textView.getText().toString();
            SpannableStringBuilder builder = new SpannableStringBuilder(str);
            char[] subStrArrs = subStr.toCharArray();
            char[] txtStrArrs = str.toCharArray();
            try{
                for(int i = 0;i < txtStrArrs.length;i++) {
                    for(int k = 0;k < subStrArrs.length;k++){
                        char subChar = subStrArrs[k];
                        if(subChar == txtStrArrs[i]){
                            ForegroundColorSpan colorSpan = new ForegroundColorSpan(color);
                            builder.setSpan(colorSpan,i,i + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                            break;
                        }
                    }
                }
            }catch (Exception e){

            }
            textView.setText(builder);
        }
    }

    /**
     * 设置文本单个或多个字段变色
     * @param textView
     * @param color 要变的颜色
     * @param subStrs 子文本集
     */
    public static void setMultipleTextFieldChangeColor(final TextView textView,final int color,String... subStrs) {
        if(subStrs != null && subStrs.length > 0){
            String str = textView.getText().toString();
            SpannableStringBuilder builder = new SpannableStringBuilder(str);
            try{
                for(int i = 0;i < subStrs.length;i++) {
                    String subStr = subStrs[i];
                    int index = str.indexOf(subStr);
                    if(index >= 0){
                        ForegroundColorSpan colorSpan = new ForegroundColorSpan(color);
                        builder.setSpan(colorSpan,index,index + subStr.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    }
                }
            }catch (Exception e){

            }
            textView.setText(builder);
        }
    }

    /**
     * TextView部分文字下划线并响应点击事件
     * @param tv
     * @param str 原始文本
     * @param subStr 需要变色的文本
     * @param color 要设置的颜色
     * @param isSetUnderline 是否设置下划线
     * @param spanClickListener 单击回调
     */
    public static void setTextHyperLink(final TextView tv, String str, String subStr,final int color,final boolean isSetUnderline,final SpanClickListener spanClickListener) {
        int index = str.indexOf(subStr);
        SpannableString spanText = new SpannableString(str);
        spanText.setSpan(new ClickableSpan() {
            @Override
            public void updateDrawState(TextPaint ds) {
                super.updateDrawState(ds);
                ds.setColor(color);//设置文件颜色
                ds.setUnderlineText(isSetUnderline);//是否设置下划线
            }
            @Override
            public void onClick(View view) {
                if(spanClickListener != null){
                    spanClickListener.onSpanClick(view);
                }
            }
        }, index,index + subStr.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        tv.setHighlightColor(Color.TRANSPARENT); //设置点击后的颜色为透明，否则会一直出现高亮
        tv.setText(spanText);
        tv.setMovementMethod(LinkMovementMethod.getInstance());//开始响应点击事件
    }
}
